/**
  * Author: Mei
  * Date: 2021-01-05 15:06
  * Description: weak_ptr一般是和shared_ptr一起使用的 主要是为了监视shared_ptr的生命周期，更像是shared_ptr的一个助手
  *
  */
#include <memory>
#include <iostream>
#include <functional>

using namespace std;

weak_ptr<int> gw;
void f() {
    if(gw.expired()) {
        cout << "gw is expired" << endl;
    } else {
        auto spt = gw.lock();
        cout << *spt << endl;
    }
}

int main() {
    shared_ptr<int> sp(new int(10));
    shared_ptr<int> sp1 = sp;
    weak_ptr<int> wp(sp);
    cout << "reference count: "<<wp.use_count() << endl;
    if(wp.expired()) {
        cout << "has been release" << endl;
    } else {
        cout << "weak_ptr is valid" << endl;
    }
    //通过lock方法来获取所监视的shared_ptr
    {
        auto sp = make_shared<int>(42);
        gw = sp;
        f();
    }
    f();
}